package com.hd.vue2204.fm.controller;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hd.vue2204.fm.entity.Category;
import com.hd.vue2204.fm.entity.IndexImg;
import com.hd.vue2204.fm.entity.Product;
import com.hd.vue2204.fm.entity.ProductImg;
import com.hd.vue2204.fm.mapper.ProductImgMapper;
import com.hd.vue2204.fm.mapper.ProductMapper;
import com.hd.vue2204.fm.service.CategoryService;
import com.hd.vue2204.fm.service.IndexImgService;
import com.hd.vue2204.fm.service.ProductImgService;
import com.hd.vue2204.fm.service.ProductService;
import com.hd.vue2204.fm.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 轮播图  前端控制器
 * </p>
 *
 * @author hd
 * @since 2023-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/index")
public class IndexImgController {
    @Autowired
    IndexImgService indexImgService;
    @Autowired
    CategoryService categoryService;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ProductMapper productDao;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ProductImgMapper productImgDao;
    @Autowired
    ProductService productService;
    @Autowired
    ProductImgService productImgService;
    //1. 首页轮播图接口
    @GetMapping("/indeximg")
    public HashMap<String,Object> index(){
        HashMap<String,Object> map=new HashMap<>();
        List<IndexImg> imgList = indexImgService.list(new QueryWrapper<IndexImg>().orderByAsc("img_id").last("limit 4"));
        if (imgList==null){
            map.put("code",-1);
        }else {
            map.put("msg","success");
            map.put("code", Constants.SUCCESS_CODE);
            map.put("data",imgList);
        }
        return map;
    }
    //2. 分类信息接口 bd继承
    @GetMapping("/category-list")
    public HashMap<String,Object> category(){
        HashMap<String, Object> map = new HashMap<>();
        List<Category> categories = categoryService.list(new QueryWrapper<Category>().eq("category_level",1));
        for (Category category : categories) {
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id",category.getCategoryId());
            List<Category> categoryList = categoryService.list(wrapper);
            category.setCategories(categoryList);
            for (Category category1 : categoryList) {
                QueryWrapper<Category> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("parent_id",category1.getCategoryId());
                List<Category> list = categoryService.list(wrapper1);
                category1.setCategories(list);
            }
        }
        if (categories ==null){
            map.put("code",-1);
        }else {
            map.put("msg","success");
            map.put("code", Constants.SUCCESS_CODE);
            map.put("data",categories);
        }
        return map;
    }
    //2. 分类信息接口 mapper
    @GetMapping("/category-list2")
    public HashMap<String,Object> category1(){
        HashMap<String, Object> map = new HashMap<>();
        List<Category> categoryList = categoryService.getCategory();
        if (categoryList ==null){
            map.put("code",-1);
        }else {
            map.put("msg","success");
            map.put("code", Constants.SUCCESS_CODE);
            map.put("data",categoryList);
        }
        return map;
    }
    //3. 新品推荐接口
    @GetMapping("/list-recommends")
    public Map<String, Object> listRecommends() {
        Map<String, Object> map = new HashMap<>();
        List<Product> products = productService.topThree();
        if (products ==null){
            map.put("code",-1);
        }else {
            map.put("msg","success");
            map.put("code", Constants.SUCCESS_CODE);
            map.put("data",products);
        }
        return map;
    }
    //4. 分类推荐mapper
    @GetMapping("/category-recommends2")
    public HashMap<String, Object> categoryRecommends() {
        HashMap<String, Object> map = new HashMap<>();
        List<Category> recommends = categoryService.getRecommends();
        if (recommends ==null){
            map.put("code",-1);
        }else {
            map.put("msg","success");
            map.put("code", Constants.SUCCESS_CODE);
            map.put("data",recommends);
        }
        return map;
    }
    //5. 分类推荐2 mp继承
    @GetMapping("/category-recommends")
    public HashMap<String, Object> categoryRecommends2() {
        HashMap<String, Object> map = new HashMap<>();
        List<Category> recommends = categoryService.list(new QueryWrapper<Category>().eq("category_level",1));
        for (Category recommend : recommends) {
            List<Product> product = productService.list(new QueryWrapper<Product>().eq("root_category_id",recommend.getCategoryId()));
            for (Product product1 : product) {
                List<ProductImg> productImgList = productImgService.list(new QueryWrapper<ProductImg>().eq("item_id",product1.getProductId()));
                product1.setImgs(productImgList);
            }
            recommend.setProducts(product);
        }
        if (recommends ==null){
            map.put("code",-1);
        }else {
            map.put("msg","success");
            map.put("code", Constants.SUCCESS_CODE);
            map.put("data",recommends);
        }
        return map;
    }


}
